<template>
  <x-dialog
    :visible="visible"
    :title="visibleType=='edit'?'编辑':'新增'"
    @close="handleClose(false)"
  >
    <el-form ref="form" inline :model="form" :rules="rules" label-width="80px">
      <el-form-item label="账号" prop="account">
        <el-input v-model="form.account" clearable />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="form.password" :type="passwordType" />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>
      <el-form-item label="姓名" prop="userName">
        <el-input v-model="form.userName" clearable />
      </el-form-item>
      <el-form-item label="手机号">
        <el-input v-model="form.userPhone" clearable />
      </el-form-item>
      <el-form-item label="邮箱">
        <el-input v-model="form.email" clearable />
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-radio-group v-model="form.sex">
          <el-radio :label="1">男</el-radio>
          <el-radio :label="0">女</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="状态">
        <el-switch
          v-model="form.status"
          :active-value="1"
          :inactive-value="0"
        />
      </el-form-item>
      <el-form-item label=" ">
        <el-button type="primary" @click="onSubmit">保存</el-button>
        <el-button @click="handleClose">取消</el-button>
      </el-form-item>
    </el-form>
  </x-dialog>
</template>

<script>
import * as axiosApi from '../api'
export default {
  props: {
    visible: Boolean,
    visibleType: {
      type: String,
      default: ''
    },
    model: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      passwordType: 'password',
      form: {
        userName: '',
        account: '',
        password: '',
        userPhone: '',
        sex: '',
        email: '',
        status: ''
      },
      rules: {
        userName: [
          { required: true, message: '请输入姓名' }
        ],
        account: [
          { required: true, message: '请输入账号' }
        ],
        password: [
          { required: true, message: '请输入密码' }
        ],
        sex: [
          { required: true, message: '选择性别' }
        ]
      }
    }
  },
  watch: {
    visible(newValue, oldValue) {
      if (newValue) {
        Object.keys(this.form).forEach(key => {
          if (this.model[key]) this.form[key] = this.model[key]
        })
      } else {
        this.initForm()
      }
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
    },
    handleClose(update) {
      this.$emit('close', update)
    },
    initForm() {
      Object.keys(this.form).forEach(item => {
        this.form[item] = ''
      })
    },
    onSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          const fun = this.visibleType === 'edit' ? axiosApi.updateInfor : axiosApi.addUser
          const params = {
            ...this.form
          }
          if (this.visibleType === 'edit')params._id = this.model._id
          fun(params).then(() => {
            this.$message({
              message: this.visibleType === 'edit' ? '更新成功' : '新增成功'
            })

            this.handleClose(true)
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
$dark_gray:#889aa4;
.show-pwd {
    position: absolute;
    right: 10px;
    top: 2px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
</style>
